home *** CD-ROM | disk | FTP | other *** search
- #define DEBUG 0
- /*
- TOWNS囲碁棋譜記録プログラム KIFFILE.C
- 1992/04/06 久保田俊也
- */
- /*
- 機能
- kiffileの読み込み
- kiffileの書き込み
- KIF_HEADERの読み込み
- KIF_HEADERの書き込み
- KIF_TEの読み込み(コメント付)
- KIF_TEの書き込み(コメント付)
-
- 記憶域の割当機能まだ未完成
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "igo.h"
- #include "banx.h"
- #include "kiffile.h"
- #include "title.h"
-
- int kiffile_read(char fname[]);
- int kiffile_write(char fname[]);
- int kiftitle_read();
- int kiftitle_write();
- int kifte_write(TE_ARG kiffile_te);
- char *comment_read(int comment_no);
- void *my_malloc(size_t size);
-
- int te_no = 0;
- void *alloc;
- TE_ARG *te_p2;
- static FILE *fp;
-
- static KIF_HV33 kif_hv33;
- TE_ARG kiffile2[MAX_TE_NUMBER];
-
- int kiffile_read(char fname[])
- {
-
- if((fp=fopen(fname,"rb"))==NULL){
- printf("read open error!\n");
- return -1;
- }
-
- #if DEBUG
- printf("kiffile read\n");
- #endif
-
- fread(&kif_hv33, sizeof(KIF_HV33), 1, fp);
-
- if(kif_hv33.file_id[0] == 'K' &&
- kif_hv33.file_id[1] == 'I' &&
- kif_hv33.file_id[2] == 'F' &&
- kif_hv33.file_id[3] == '3' ){
-
- alloc = my_malloc(kif_hv33.te_number*sizeof(TE_ARG));
- if(alloc == NULL){
- printf("my_malloc error!\n");
- return -1;
- }
-
- fread(alloc, sizeof(TE_ARG), kif_hv33.te_number, fp);
-
- }
-
- kiftitle_read();
- te_no = 0;
- te_p2 = (TE_ARG *)alloc;
-
- return 0;
- }
-
- static int kiftitle_read()
- {
- BAN_TYPE ban_type = { NORMAL, 19, 0};
-
- title_tenumber_set(kif_hv33.te_number);
- title_playstart_time_set(kif_hv33.play_start);
- title_playend_time_set(kif_hv33.play_end);
- title_handy_set(kif_hv33.handy);
- title_komiset( kif_hv33.komi_id, kif_hv33.komi_number, kif_hv33.hanmoku_id);
- title_blacknameset( kif_hv33.player_black);
- title_whitenameset( kif_hv33.player_white);
- title_playspaceset(kif_hv33.play_space);
- title_issueset( kif_hv33.vicdef_id, kif_hv33.vicdef_number);
- if(kif_hv33.ver == 2){
- title_bantype_set( ban_type);
- }else{
- title_bantype_set( kif_hv33.ban_type);
- }
-
- return 0;
- }
-
- int kiffile_comment_read()
- {
- int i, c;
- char comment[256];
-
- i=0;
- while((c = getc(fp))!=EOF){
- if((comment[i] = (char)c) == '\0'){
- comment_set(comment);
- i=0;
- }else{
- i++;
- }
- }
-
- if(fclose(fp)==EOF){
- printf("read close error!\n");
- return -1;
- }
-
- return 0;
- }
-
- int kifte_read2(TE_ARG *kiffile_te)
- {
- *kiffile_te = *te_p2;
- te_no++;
- te_p2++;
- if(te_no == kif_hv33.te_number){
-
- my_free(alloc);
- return(1);
-
- }
- return(0);
-
- }
-
- int kiftitle_write()
- {
-
- kif_hv33.ver = title_ver_read();
- kif_hv33.play_start = *title_playstart_time_read();
- kif_hv33.play_end = *title_playend_time_read();
- kif_hv33.handy = title_handy_read();
- title_komiread( &kif_hv33.komi_id,
- &kif_hv33.komi_number,
- &kif_hv33.hanmoku_id);
- title_blacknameread( kif_hv33.player_black);
- title_whitenameread( kif_hv33.player_white);
- title_playspaceread( kif_hv33.play_space);
- title_issueread( &kif_hv33.vicdef_id,
- &kif_hv33.vicdef_number);
- kif_hv33.ban_type = *title_bantype_read();
-
- te_no = 0;
-
- return 0;
- }
-
- int kifte_write(TE_ARG kiffile_te2)
- {
- kiffile2[te_no] = kiffile_te2;
- te_no++;
-
- return 0;
- }
-
- int kiffile_write( char fname[])
- {
- FILE *fp;
- int c, i, j;
- char *comment;
-
- kif_hv33.file_id[0] = 'K';
- kif_hv33.file_id[1] = 'I';
- kif_hv33.file_id[2] = 'F';
- kif_hv33.file_id[3] = '3';
- kif_hv33.te_number = te_no;
-
- if((fp=fopen(fname,"wb"))==NULL){
- printf("write open error!\n");
- return -1;
- }
-
- fwrite(&kif_hv33, sizeof(KIF_HEADER_VER3), 1, fp);
-
- fwrite(&kiffile2[0], sizeof(TE_ARG), te_no, fp);
-
- for(i=1;(comment=comment_read(i))!=NULL;i++){
- j=0;
- do{
- c = (int)comment[j];
- putc(c, fp);
- j++;
- }while((char)c != '\0');
- }
-
- fclose(fp);
-
- return 0;
- }
-